package com.example.egmvc1.controller;

import com.example.egmvc1.mapper.AddressMapper;
import com.example.egmvc1.pojo.dto.AddressAddParam;
import com.example.egmvc1.pojo.entity.Address;
import com.example.egmvc1.pojo.vo.AddressInfoVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@Slf4j
@RestController
@Api(tags = "02.地址管理模块")
@RequestMapping("/v1/address")
public class AddressController {
    @Autowired
    private AddressMapper addressMapper;

    @PostMapping("/add")
    @ApiOperation(value = "添加地址")
    @ApiOperationSupport(order = 10)
    public String addAddress(AddressAddParam addressAddParam) {
        Address address = new Address();
        BeanUtils.copyProperties(addressAddParam, address);
        log.debug("address:" + address);
        addressMapper.addAddress(address);
        return "添加成功";
    }

    @GetMapping("/list")
    @ApiOperation(value = "查询地址列表")
    @ApiOperationSupport(order = 20)
    public List<AddressInfoVO> listAddress() {
        return addressMapper.listAddress();
    }

    @PostMapping("/del")
    @ApiOperation(value = "删除地址")
    @ApiOperationSupport(order = 30)
    @ApiImplicitParam(name = "id", value = "地址id",dataType ="int")
    public String delAddress(Integer id) {
        addressMapper.delAddress(id);
        return "删除成功";
    }
}
